function [l1,l2,l3,v1,v2,v3,v4,v5,v6,v7,v8,v9] ...
        = eigen_sort3d_m(l1,l2,l3,v1,v2,v3,v4,v5,v6,v7,v8,v9)

%% setup
if nargin<12
    v1 = []; v2 = []; v3 = []; v4 = []; v5 = []; 
    v6 = []; v7 = []; v8 = []; v9 = [];
end

%% eigen values
l123 = [];
l123(:,:,:,1) = abs(l1); 
l123(:,:,:,2) = abs(l2); 
l123(:,:,:,3) = abs(l3);
%[L123,idx] = sort(L123,4,'descend');
[l123,idx] = sort(l123,4);

l1s = l1; 
l2s = l2;
l3s = l3;
l1s(idx(:,:,:,1)==2) = l2(idx(:,:,:,1)==2);
l1s(idx(:,:,:,1)==3) = l3(idx(:,:,:,1)==3);
l2s(idx(:,:,:,2)==1) = l1(idx(:,:,:,2)==1);
l2s(idx(:,:,:,2)==3) = l3(idx(:,:,:,2)==3);
l3s(idx(:,:,:,3)==1) = l1(idx(:,:,:,3)==1);
l3s(idx(:,:,:,3)==2) = l2(idx(:,:,:,3)==2);
l1 = l1s;
l2 = l2s;
l3 = l3s;

%% eigen vectors
if ~isempty(v1)
v1s = v1; 
v2s = v2;
v3s = v3;
v4s = v4;
v5s = v5;
v6s = v6;
v7s = v7;
v8s = v8;
v9s = v9;

% v123 v456 v789
v1s(idx(:,:,:,1)==2) = v4(idx(:,:,:,1)==2);
v2s(idx(:,:,:,1)==2) = v5(idx(:,:,:,1)==2);
v3s(idx(:,:,:,1)==2) = v6(idx(:,:,:,1)==2);

v1s(idx(:,:,:,1)==3) = v7(idx(:,:,:,1)==3);
v2s(idx(:,:,:,1)==3) = v8(idx(:,:,:,1)==3);
v3s(idx(:,:,:,1)==3) = v9(idx(:,:,:,1)==3);

v4s(idx(:,:,:,2)==1) = v1(idx(:,:,:,2)==1);
v5s(idx(:,:,:,2)==1) = v2(idx(:,:,:,2)==1);
v6s(idx(:,:,:,2)==1) = v3(idx(:,:,:,2)==1);

v4s(idx(:,:,:,2)==3) = v7(idx(:,:,:,2)==3);
v5s(idx(:,:,:,2)==3) = v8(idx(:,:,:,2)==3);
v6s(idx(:,:,:,2)==3) = v9(idx(:,:,:,2)==3);

v7s(idx(:,:,:,3)==1) = v1(idx(:,:,:,3)==1);
v8s(idx(:,:,:,3)==1) = v2(idx(:,:,:,3)==1);
v9s(idx(:,:,:,3)==1) = v3(idx(:,:,:,3)==1);

v7s(idx(:,:,:,3)==2) = v4(idx(:,:,:,3)==2);
v8s(idx(:,:,:,3)==2) = v5(idx(:,:,:,3)==2);
v9s(idx(:,:,:,3)==2) = v6(idx(:,:,:,3)==2);

v1 = v1s;
v2 = v2s;
v3 = v3s;
v4 = v4s;
v5 = v5s;
v6 = v6s;
v7 = v7s;
v8 = v8s;
v9 = v9s;
end

end